home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / syscalls / link / linkTest.c < prev   
Encoding:
C/C++ Source or Header  |  1990-05-31  |  4.4 KB  |  245 lines

  1. /* 
  2.  * linkTest.c --
  3.  *
  4.  *    Test the link system call.
  5.  *
  6.  * Copyright 1990 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header$";
  18. #endif /* not lint */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24.  
  25. extern int errno;
  26. static int errors;
  27.  
  28. static char tempfile1[MAXPATHLEN];
  29. static char tempfile2[MAXPATHLEN];
  30.  
  31. /*
  32.  *----------------------------------------------------------------------
  33.  *
  34.  * main --
  35.  *
  36.  *
  37.  * Results:
  38.  *    Exits with a zero exit status if everything works properly,
  39.  *      non-zero otherwise.
  40.  *
  41.  * Side effects:
  42.  *
  43.  *----------------------------------------------------------------------
  44.  */
  45.  
  46. void
  47. main(void)
  48. {
  49.  
  50.     /* 
  51.      *      Create and open a couple temporary files.
  52.      */
  53.     if (tmpnam(tempfile1) != tempfile1) {
  54.     (void) fprintf(stderr, "tmpnam() returned an incorrect value.\n");
  55.     exit(EXIT_FAILURE);
  56.     }
  57.     if (tmpnam(tempfile2) != tempfile2) {
  58.     (void) fprintf(stderr, "tmpnam() returned an incorrect value.\n");
  59.     exit(EXIT_FAILURE);
  60.     }
  61.  
  62.     /*
  63.      *      Neither name exists, should fail.
  64.      */
  65.     if (link(tempfile1, tempfile2) == 0) {
  66.  
  67.  
  68.     }
  69.  
  70.     /*
  71.      *      create tempfile1
  72.      */
  73.     if ((fd = open(tempfile1, O_RDWR|O_CREAT, 0666)) < 0) {
  74.     (void) fprintf(stderr, "Cannot open %s: %s\n",
  75.         tempfile1, strerror(errno));
  76.     exit(EXIT_FAILURE);
  77.     }
  78.  
  79.     /*
  80.      *      First name exists, second does not.  Should succeed.
  81.      */
  82.     if (link(tempfile1, tempfile2) != 0) {
  83.     (void) fprintf(stderr, "link %s to %s failed: %s\n",
  84.         tempfile1, tempfile2, strerror(errno));
  85.  
  86.  
  87.     }
  88.  
  89.  
  90.  
  91.    /*
  92.     *       Open both links.  Write to one, read from the other
  93.     *       and make sure the data matches.
  94.     */
  95.  
  96.  
  97.     /*
  98.      *      Both names exist, should fail.
  99.      */
  100.     if (link(tempfile1, tempfile2) {
  101.  
  102.  
  103.     }
  104.  
  105.  
  106.     /*
  107.      *      Stat both links and make sure the link count is 2
  108.      *      for both links.
  109.      */
  110.     if (stat(tempfile1, &statbuf) {
  111.  
  112.  
  113.     }
  114.     if (statbuf.st_nlink != 2) {
  115.  
  116.  
  117.     }
  118.     if (stat(tempfile2, &statbuf) {
  119.  
  120.  
  121.     }
  122.     if (statbuf.st_nlink != 2) {
  123.  
  124.  
  125.     }
  126.  
  127.     /*
  128.      *      Unlink the first (original) file, then make
  129.      *      sure the second still accesses the file.
  130.      */
  131.     if (unlink(tempfile1)) {
  132.  
  133.  
  134.     }
  135.  
  136.  
  137.     /*
  138.      *      Try a too long name
  139.      *      Should fail.
  140.      */
  141.     memset(longname, 'a', sizeof(longname) - 1);
  142.     if (link(tempfile1, longname) == 0) {
  143.  
  144.  
  145.     }
  146.  
  147.     /*
  148.      *      Open a file in /usr/tmp and /tmp.
  149.      *      Check the inodes to make sure they are on different
  150.      *      devices.  If they are then try to create a hard link
  151.      *      between them.  Should fail.
  152.      */
  153.  
  154.     if ((fd1 = open("/tmp/linkTest.tmp", O_RDWR|
  155.  
  156.  
  157.  
  158.  
  159.     /*
  160.      *      Try a filename containing a character with a high order bit set.
  161.      *      Should fail.
  162.      */
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.     exit((errors == 0) ? EXIT_SUCCESS: EXIT_FAILURE);
  171. }
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188. /*
  189.  *----------------------------------------------------------------------
  190.  *
  191.  * testExit --
  192.  *
  193.  *    Fork off a child process that exits with the given exit code.
  194.  *      Wait for the child to exit.  Check the return status and make
  195.  *      sure that it matches the exit status.
  196.  *
  197.  * Results:
  198.  *    none.
  199.  *
  200.  * Side effects:
  201.  *      Prints a message to stderr nad increments `errors' if there is
  202.  *      an error.
  203.  *
  204.  *----------------------------------------------------------------------
  205.  */
  206.  
  207. static void
  208. testExit(exitCode)
  209.     int exitCode;
  210. {
  211.     int w;
  212.     int child;
  213.     union wait ws;
  214.  
  215.     switch (child = fork()) {
  216.  
  217.     case 0:
  218.     exit(exitCode);
  219.     (void) fprintf(stderr, "Exit returned: %s\n", strerror(errno));
  220.     abort();
  221.     (void) fprintf(stderr, "Abort returned: %s\n", strerror(errno));
  222.     for (;;) {
  223.         continue;
  224.     }
  225.  
  226.     case -1:
  227.     (void) fprintf(stderr, "Fork failed: %s\n", strerror(errno));
  228.     ++errors;
  229.     break;
  230.  
  231.     default:
  232.     while ((w = wait(&ws)) > 0 && w != child) {
  233.         continue;
  234.     }
  235.     if (ws.w_retcode != exitCode) {
  236.         (void) fprintf(stderr, "Exit returned %d, instead of %d.\n",
  237.         ws.w_retcode, exitCode);
  238.         ++errors;
  239.     }
  240.     break;
  241.     }
  242.     return;
  243. }
  244.  
  245.